var Reply_message = {
    initialize : function(settings) {
        layui.use(['form','element','layedit','table'], function(){
            var form = layui.form,
                element = layui.element,
                layedit = layui.layedit,
                table = layui.table;

            table.render({
                elem: '#reply_message'
                ,even: true
                ,url:Base.url('page')
                ,cols: [[
                    {type:'checkbox', fixed: 'left'}
                    ,{field:'id', title: 'ID', sort: true}
                    ,{field:'reply_commenter', title: '回复留言者'}
                    ,{field:'addtime', title: '回复留言时间', sort: true}
                    ,{field:'reply_master', title: '被留言者'}
                    ,{field:'identifier',title: '留言标识'}
                    ,{field:'content', title: '留言内容', templet: '#content'}
                    ,{field:'upvote', title: '点赞次数', width:100, sort: true}
                    ,{field:'auditing', title: '审核', templet: '#switchTpl', unresize: true}
                    ,{title: '操作',align:'center', toolbar: '#barDemo'}
                ]]
                ,page: true
            });

            $(document).on({
                mouseenter:function () {
                    var content = $(this).parents('td').attr('data-content');
                    if(typeof content != 'undefined'){
                        layer.tips('<span class="tips">'+content+'</span>',this,{tips: [1, '#fff'],time:122222220});
                    }
                },
                mouseleave:function () {
                    layer.closeAll('tips');
                }

            },".layui-table>tbody>tr td a");

            //删除
            table.on('tool(reply_message)', function(obj){
                var data = obj.data;
                if(obj.event === 'del'){
                    layer.confirm('你确定要删除回复留言吗？', function(index){
                        Reply_message.delete_message(data.id, data.identifier);
                        layer.close(index);
                    });
                }
            });

            //操作
            var active = {
                auditing_messages: function(){ //批量审核分类
                    var checkStatus = table.checkStatus('reply_message'),
                        data = checkStatus.data;
                    Reply_message.auditings(data);
                }
                ,delete_messages: function(){ //批量删除分类
                    var checkStatus = table.checkStatus('reply_message'),
                        data = checkStatus.data;
                    Reply_message.delete_messages(data);
                }
            };

            $('.operateTable .layui-btn').on('click', function(){
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });

            //是否审核
            form.on('switch(auditing)', function(data){
                var checkbox = data.elem,
                    dataId = data.value;
                if(checkbox.checked){
                    Reply_message.auditing(dataId, 1);
                }else{
                    Reply_message.auditing(dataId, 0);
                }
            });   

        });
    },

    //删除评论
    delete_message : function(id, identifier){
        var datas = {id:id,identifier:identifier};
        layer.confirm('你确定要删除回复留言吗？',{
            icon:3,
            btn:['确定','取消'],
            yes: function(index){
                Base.ajax({
                    url :Base.url('delete_reply_message'),
                    type:'post',
                    data : {data:datas},
                    success : function(data) {
                        if (data.status == 'ok') {
                            layer.msg(data.results,{time:1000},function(){
                                layer.closeAll();
                                Base.redirect(Base.url('reply_message'));
                            });
                        } else {
                            layer.msg(data.errdesc,{time:1500});
                            layer.closeAll('loading');
                            return false;
                        }
                    }
                });
            }
        });
    },

    delete_messages : function(data){
        var datas = [];
        $.each(data, function(index,val){
            datas.push({id:val['id'],identifier:val['identifier']});
        });

        if(datas.length == 0){
            layer.msg('请选择要删除的回复留言',{time:2000});
            return false;
        }

        layer.confirm('你确定要删除这些回复留言吗？',{
            icon:3,
            btn:['确定','取消'],
            yes: function(index){
                Base.ajax({
                    url :Base.url('delete_reply_message'),
                    type:'post',
                    data : {data:datas},
                    success : function(data) {
                        if (data.status == 'ok') {
                            layer.msg(data.results,{time:1000},function(){
                                layer.closeAll();
                                Base.redirect(Base.url('reply_message'));
                            });
                        } else {
                            layer.msg(data.errdesc,{time:1500});
                            layer.closeAll('loading');
                            return false;
                        }
                    }
                });
            }
        });
    },

    //是否审核
    auditing : function(id, audit){
        var audit = parseInt(audit),
            datas = {id:id,auditing:audit};
        if(audit){
            layer.load(3);
            Base.ajax({
                url :Base.url('edit_reply_message'),
                type:'post',
                data : {data:datas},
                success : function(data) {
                    if (data.status == 'ok') {
                        layer.msg('更改成功',{time:1000});
                        layer.closeAll('loading');
                    } else {
                        layer.msg(data.errdesc,{time:1500});
                        layer.closeAll('loading');
                        return false;
                    }
                }
            });
        }else{
            layer.load(3);
            Base.ajax({
                url :Base.url('edit_reply_message'),
                type:'post',
                data : {data:datas},
                success : function(data) {
                    if (data.status == 'ok') {
                        layer.msg('更改成功',{time:1000});
                        layer.closeAll('loading');
                    } else {
                        layer.msg(data.errdesc,{time:1500});
                        layer.closeAll('loading');
                        return false;
                    }
                }
            });
        } 
        
    },

    //批量审核
    auditings : function(data){
        var datas = [];
        $.each(data, function(index,val){
            datas.push({id:val['id'],auditing:1});
        });

        if(datas.length == 0){
            layer.msg('请选择审核的回复留言',{time:2000});
            return false;
        }

        layer.confirm('你确定要审核这些回复留言吗？',{
            icon:3,
            btn:['确定','取消'],
            closeBtn:2,
            yes: function(index){
                layer.load(3);
                Base.ajax({
                    url :Base.url('edit_reply_message'),
                    type:'post',
                    data : {data:datas},
                    success : function(data) {
                        if (data.status == 'ok') {
                            layer.msg('审核成功',{time:1000},function(){
                                layer.closeAll();
                                Base.redirect(Base.url('reply_message'));
                            });
                        } else {
                            layer.msg(data.errdesc,{time:1500});
                            layer.closeAll('loading');
                            return false;
                        }
                    }
                });
            }
        });
    }
}